package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.dao.AwardHistoryDao;
import com.bestcem.xm.award.dao.mapper.AwardHistoryMapper;
import com.bestcem.xm.award.entity.mysql.AwardHistoryEntity;
import com.bestcem.xm.award.entity.pojo.AwardHistoryDO;
import com.bestcem.xm.award.entity.pojo.DeliverDetailDO;
import com.bestcem.xm.award.entity.pojo.RedPacketStatsDO;
import com.bestcem.xm.award.grpc.service.param.redpacket.WinDetailListParam;
import com.bestcem.xm.common.core.uitls.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/6/29 16:57
 * @desc history记录表查询
 */
@Slf4j
@Repository
public class AwardHistoryDaoImpl implements AwardHistoryDao {

    @Resource
    AwardHistoryMapper awardHistoryMapper;

    @Override
    public List<String> getDeliverNameList(String awardId) {
        return awardHistoryMapper.getDeliverNameList(awardId);
    }

    @Override
    public long getWinCount(String awardId) {
        Long count = awardHistoryMapper.getWinCount(awardId);
        return count == null ? 0 : count;
    }

    @Override
    public long getTryDrawCount(String awardId) {
        Long count = awardHistoryMapper.getTryDrawCount(awardId);
        return count == null ? 0 : count;
    }



    @Override
    public RedPacketStatsDO getSendAmountAndCount(String awardId) {
        return awardHistoryMapper.getSendAmountAndCount(awardId);
    }

    @Override
    public List<DeliverDetailDO> getDeliverDetail(String awardId) {
        return awardHistoryMapper.getDeliverDetail(awardId);
    }

    @Override
    public long countByIdAndStatus(String awardId, List<Integer> statusList) {
        Long count = awardHistoryMapper.countByIdAndStatus(awardId, statusList);
        return count == null ? 0 : count;
    }

    @Override
    public long getWinDetailListCount(WinDetailListParam param) {
        Long count = awardHistoryMapper.getWinDetailListCount(param);
        return count == null ? 0 : count;
    }

    @Override
    public List<AwardHistoryDO> getWinDetailList(WinDetailListParam param) {
        List<AwardHistoryEntity> list = awardHistoryMapper.getWinDetailList(param);
        if (list == null) {
            return null;
        }
        List<AwardHistoryDO> doList = new ArrayList<>();
        for (AwardHistoryEntity entity : list) {
            if (entity != null) {
                AwardHistoryDO awardHistoryDO = new AwardHistoryDO();
                BeanUtils.copyProperties(entity, awardHistoryDO);
                doList.add(awardHistoryDO);
            }
        }
        return doList;
    }

    @Override
    public long countByIdAndOpenIdAndNotPass(String awardId, String openId) {
        Long count =  awardHistoryMapper.countByIdAndOpenIdAndNotPass(awardId, openId);
        return count == null ? 0 : count;
    }

    @Override
    public AwardHistoryDO findByRequest(String awardId, String deliverId, String requestId) {
        AwardHistoryEntity entity = awardHistoryMapper.findByRequest(awardId, deliverId, requestId);
        if (entity == null) {
            return null;
        }

        AwardHistoryDO awardHistoryDO = new AwardHistoryDO();
        BeanUtils.copyProperties(entity, awardHistoryDO);

        return awardHistoryDO;
    }

    @Override
    public AwardHistoryDO findOneByAwardIdAndRequestIdAndStatus(String awardId, String requestId, Integer status) {
        AwardHistoryEntity entity = awardHistoryMapper.findOneByAwardIdAndRequestIdAndStatus(awardId, requestId, status);
        if (entity == null) {
            return null;
        }
        AwardHistoryDO awardHistoryDO = new AwardHistoryDO();
        BeanUtils.copyProperties(entity, awardHistoryDO);
        return awardHistoryDO;
    }

    @Override
    public int updateWinInfoByAwardIdAndOrderId(String awardId, String orderId, Integer amount, Integer status) {
        return awardHistoryMapper.updateWinInfoByAwardIdAndOrderId(awardId, orderId, amount, status);
    }


    @Override
    public int insertSelective(AwardHistoryDO awardHistoryDO) {
        if (awardHistoryDO == null) {
            return 0;
        }
        AwardHistoryEntity entity = new AwardHistoryEntity();
        BeanUtils.copyProperties(awardHistoryDO, entity);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());
        return awardHistoryMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(AwardHistoryDO awardHistoryDO) {
        if (awardHistoryDO == null) {
            return 0;
        }
        AwardHistoryEntity entity = new AwardHistoryEntity();
        BeanUtils.copyProperties(awardHistoryDO, entity);
        entity.setUpdateTime(DateUtil.getCommonDate());
        return awardHistoryMapper.updateByPrimaryKeySelective(entity);
    }

}
